package com.fuxi.cloud.desreport.b;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fuxi.cloud.desreport.model.ReportRow;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * a
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-05-03
 */
public class e {
    private static final Logger a = LoggerFactory.getLogger(e.class);
    private static final String b = "<#list";
    private static final String c = "</#list>";
    private String d;
    private String e;
    private boolean f;
    private Object g;
    private List<String> h;
    private List<String> i;
    private List<String> j;
    private List<String> k;
    private List<ReportRow> l;

    public e() {
    }

    public e(String var1, String var2, boolean var3) {
        this.d = var1;
        this.e = var2;
        this.f = var3;
        this.h = new ArrayList();
        this.k = new ArrayList();
        this.l = new ArrayList();
        this.j = new ArrayList();
        this.i = new ArrayList();
        this.a();
    }

    public e(String var1, String var2) {
        this(var1, var2, var2.indexOf("<#list") >= 0);
    }

    public void a() {
        if (this.f) {
            JSONObject var1 = JSON.parseObject(this.e);
            JSONObject var2 = var1.getJSONObject("rows");
            Iterator var3 = var2.keySet().iterator();

            while(var3.hasNext()) {
                String var4 = (String)var3.next();
                String var5 = var2.getString(var4);
                if (var5.indexOf("<#list") > 0) {
                    this.h.add(var4);
                }

                if (var5.indexOf("</#list>") >= 0) {
                    this.j.add(var4);
                }

                this.k.add(var4);
                ReportRow var6;
                if ("len".equals(var4)) {
                    var6 = new ReportRow(var4, var2.getIntValue(var4));
                    this.l.add(var6);
                } else {
                    var6 = new ReportRow(var4, var2.getJSONObject(var4));
                    this.l.add(var6);
                }
            }
        }

    }

    public JSONObject a(Object var1) throws IOException, TemplateException {
        Configuration var2 = this.getConfiguration();
        JSONObject var3 = new JSONObject();
        var3.put("rows", this.l);
        String var4 = "circle";
        Template var5 = new Template(var4, var3.toJSONString(), var2);
        StringWriter var6 = new StringWriter();
        var5.process(var1, var6);
        JSONObject var7 = JSON.parseObject(var6.toString());
        a.debug("循环处理结果>>" + var6.toString());
        List var8 = JSONArray.parseArray(var7.getString("rows"), ReportRow.class);
        JSONObject var9 = new JSONObject();
        int var10 = -1;
        int var11 = 0;
        Iterator var12 = var8.iterator();

        while(var12.hasNext()) {
            ReportRow var13 = (ReportRow)var12.next();
            String var14 = var13.getIndex();
            if ("len".equals(var14)) {
                var9.put(var14, var13.getContent());
            } else {
                int var15 = Integer.parseInt(var14);
                if (this.j.indexOf(var14) >= 0) {
                    --var11;
                } else if (this.h.indexOf(var14) >= 0) {
                    if (this.i.indexOf(var14) < 0) {
                        --var11;
                        this.i.add(var14);
                    }

                    var10 = var15 + 1;
                } else {
                    String var16 = var15 + var11 + "";
                    var9.put(var16, var13.getContent());
                    if (var10 == var15) {
                        ++var11;
                    }
                }
            }
        }

        return var9;
    }

    public String b(Object var1) throws IOException, TemplateException {
        Configuration var2 = this.getConfiguration();
        Template var3 = new Template(this.d, this.e, var2);
        StringWriter var4 = new StringWriter();
        var3.process(var1, var4);
        String var5 = var4.toString();
        JSONObject var6 = JSON.parseObject(var5);
        if (this.f) {
            JSONObject var7 = this.a(var1);
            var6.put("rows", var7);
        }

        return var6.toJSONString();
    }

    private Configuration getConfiguration() {
        Configuration var1 = new Configuration(Configuration.VERSION_2_3_28);
        StringTemplateLoader var2 = new StringTemplateLoader();
        var1.setTemplateLoader(var2);
        var1.setDefaultEncoding("UTF-8");
        return var1;
    }

    public String getTemplateName() {
        return this.d;
    }

    public String getJsonStr() {
        return this.e;
    }

    public boolean b() {
        return this.f;
    }

    public Object getModel() {
        return this.g;
    }

    public List<String> getCircleList() {
        return this.h;
    }

    public List<String> getCircleDiffList() {
        return this.i;
    }

    public List<String> getCircleEndList() {
        return this.j;
    }

    public List<String> getRowKeyList() {
        return this.k;
    }

    public List<ReportRow> getRowContentList() {
        return this.l;
    }

    public void setTemplateName(String templateName) {
        this.d = templateName;
    }

    public void setJsonStr(String jsonStr) {
        this.e = jsonStr;
    }

    public void setCircle(boolean circle) {
        this.f = circle;
    }

    public void setModel(Object model) {
        this.g = model;
    }

    public void setCircleList(List<String> circleList) {
        this.h = circleList;
    }

    public void setCircleDiffList(List<String> circleDiffList) {
        this.i = circleDiffList;
    }

    public void setCircleEndList(List<String> circleEndList) {
        this.j = circleEndList;
    }

    public void setRowKeyList(List<String> rowKeyList) {
        this.k = rowKeyList;
    }

    public void setRowContentList(List<ReportRow> rowContentList) {
        this.l = rowContentList;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof e)) {
            return false;
        } else {
            e var2 = (e)o;
            if (!var2.c(this)) {
                return false;
            } else if (this.b() != var2.b()) {
                return false;
            } else {
                label109: {
                    String var3 = this.getTemplateName();
                    String var4 = var2.getTemplateName();
                    if (var3 == null) {
                        if (var4 == null) {
                            break label109;
                        }
                    } else if (var3.equals(var4)) {
                        break label109;
                    }

                    return false;
                }

                label102: {
                    String var5 = this.getJsonStr();
                    String var6 = var2.getJsonStr();
                    if (var5 == null) {
                        if (var6 == null) {
                            break label102;
                        }
                    } else if (var5.equals(var6)) {
                        break label102;
                    }

                    return false;
                }

                Object var7 = this.getModel();
                Object var8 = var2.getModel();
                if (var7 == null) {
                    if (var8 != null) {
                        return false;
                    }
                } else if (!var7.equals(var8)) {
                    return false;
                }

                label88: {
                    List var9 = this.getCircleList();
                    List var10 = var2.getCircleList();
                    if (var9 == null) {
                        if (var10 == null) {
                            break label88;
                        }
                    } else if (var9.equals(var10)) {
                        break label88;
                    }

                    return false;
                }

                List var11 = this.getCircleDiffList();
                List var12 = var2.getCircleDiffList();
                if (var11 == null) {
                    if (var12 != null) {
                        return false;
                    }
                } else if (!var11.equals(var12)) {
                    return false;
                }

                label74: {
                    List var13 = this.getCircleEndList();
                    List var14 = var2.getCircleEndList();
                    if (var13 == null) {
                        if (var14 == null) {
                            break label74;
                        }
                    } else if (var13.equals(var14)) {
                        break label74;
                    }

                    return false;
                }

                List var15 = this.getRowKeyList();
                List var16 = var2.getRowKeyList();
                if (var15 == null) {
                    if (var16 != null) {
                        return false;
                    }
                } else if (!var15.equals(var16)) {
                    return false;
                }

                List var17 = this.getRowContentList();
                List var18 = var2.getRowContentList();
                if (var17 == null) {
                    if (var18 != null) {
                        return false;
                    }
                } else if (!var17.equals(var18)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean c(Object var1) {
        return var1 instanceof e;
    }

    public int hashCode() {
        boolean var1 = true;
        byte var2 = 1;
        int var11 = var2 * 59 + (this.b() ? 79 : 97);
        String var3 = this.getTemplateName();
        var11 = var11 * 59 + (var3 == null ? 43 : var3.hashCode());
        String var4 = this.getJsonStr();
        var11 = var11 * 59 + (var4 == null ? 43 : var4.hashCode());
        Object var5 = this.getModel();
        var11 = var11 * 59 + (var5 == null ? 43 : var5.hashCode());
        List var6 = this.getCircleList();
        var11 = var11 * 59 + (var6 == null ? 43 : var6.hashCode());
        List var7 = this.getCircleDiffList();
        var11 = var11 * 59 + (var7 == null ? 43 : var7.hashCode());
        List var8 = this.getCircleEndList();
        var11 = var11 * 59 + (var8 == null ? 43 : var8.hashCode());
        List var9 = this.getRowKeyList();
        var11 = var11 * 59 + (var9 == null ? 43 : var9.hashCode());
        List var10 = this.getRowContentList();
        var11 = var11 * 59 + (var10 == null ? 43 : var10.hashCode());
        return var11;
    }

    public String toString() {
        return "ReportJsonResolver(templateName=" + this.getTemplateName() + ", jsonStr=" + this.getJsonStr() + ", circle=" + this.b() + ", model=" + this.getModel() + ", circleList=" + this.getCircleList() + ", circleDiffList=" + this.getCircleDiffList() + ", circleEndList=" + this.getCircleEndList() + ", rowKeyList=" + this.getRowKeyList() + ", rowContentList=" + this.getRowContentList() + ")";
    }
}

